<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Constructs an AppendIterator</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="appenditerator.append.html">« AppendIterator::append</a></li>
      <li style="float: right;"><a href="appenditerator.current.html">AppendIterator::current »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.appenditerator.html">AppendIterator</a></li>
    <li>Constructs an AppendIterator</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="appenditerator.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">AppendIterator::__construct</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">AppendIterator::__construct</span> &mdash; <span class="dc-title">Constructs an AppendIterator</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-appenditerator.construct-description">
  <h3 class="title">说明</h3>
  <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>AppendIterator::__construct</strong></span>()</div>

  <p class="para rdfs-comment">
   Constructs an AppendIterator.
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-appenditerator.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 examples" id="refsect1-appenditerator.construct-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="appenditerator.examples.foreach">
    <p><strong>示例 #1 Iterating AppendIterator with foreach</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$pizzas&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'Margarita'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Siciliana'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Hawaii'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$toppings&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'Cheese'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Anchovies'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Olives'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Pineapple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Ham'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$appendIterator&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">AppendIterator</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$appendIterator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">append</span><span style="color: #007700">(</span><span style="color: #0000BB">$pizzas</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$appendIterator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">append</span><span style="color: #007700">(</span><span style="color: #0000BB">$toppings</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$appendIterator&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$item</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'&nbsp;=&gt;&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$item&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
0 =&gt; Margarita
1 =&gt; Siciliana
2 =&gt; Hawaii
0 =&gt; Cheese
1 =&gt; Anchovies
2 =&gt; Olives
3 =&gt; Pineapple
4 =&gt; Ham
</pre></div>
    </div>
   </div>
   <div class="example" id="appenditerator.examples.api">
    <p><strong>示例 #2 Iterating AppendIterator with the AppendIterator API</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$pizzas&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'Margarita'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Siciliana'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Hawaii'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$toppings&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'Cheese'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Anchovies'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Olives'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Pineapple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Ham'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$appendIterator&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">AppendIterator</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$appendIterator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">append</span><span style="color: #007700">(</span><span style="color: #0000BB">$pizzas</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$appendIterator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">append</span><span style="color: #007700">(</span><span style="color: #0000BB">$toppings</span><span style="color: #007700">);<br /><br />while&nbsp;(</span><span style="color: #0000BB">$appendIterator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">valid</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'%s&nbsp;=&gt;&nbsp;%s&nbsp;=&gt;&nbsp;%s%s'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$appendIterator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getIteratorIndex</span><span style="color: #007700">(),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$appendIterator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">key</span><span style="color: #007700">(),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$appendIterator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">current</span><span style="color: #007700">(),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">PHP_EOL<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$appendIterator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">next</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
0 =&gt; 0 =&gt; Margarita
0 =&gt; 1 =&gt; Siciliana
0 =&gt; 2 =&gt; Hawaii
1 =&gt; 0 =&gt; Cheese
1 =&gt; 1 =&gt; Anchovies
1 =&gt; 2 =&gt; Olives
1 =&gt; 3 =&gt; Pineapple
1 =&gt; 4 =&gt; Ham
</pre></div>
    </div>
   </div> 
  </p>
 </div>
 
 
 <div class="refsect1 notes" id="refsect1-appenditerator.construct-notes">
  <h3 class="title">注释</h3>
  <div class="caution"><strong class="caution">警告</strong>
   <p class="para">
    When using <span class="function"><a href="function.iterator-to-array.html" class="function">iterator_to_array()</a></span> to copy the values of the AppendIterator 
    into an array, you have to set the optional <code class="parameter">use_key</code> argument to 
    <strong><code>false</code></strong>. When <code class="parameter">use_key</code> is not <strong><code>false</code></strong> any keys reoccurring in inner 
    iterators will get overwritten in the returned array. There is no way to preserve the original keys.
   </p>
  </div>
 </div>
 

 <div class="refsect1 seealso" id="refsect1-appenditerator.construct-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="appenditerator.append.html" class="methodname" rel="rdfs-seeAlso">AppendIterator::append()</a> - Appends an iterator</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>